﻿BranchingTests = function () {
    this.Name = "Branching Tests";
};

BranchingTests.prototype = new TestFixture();


BranchingTests.prototype.TestBranch = function () {
    var vm = new VM();
    var commands = [];

    commands.push("ldc.i4 5");                  // 0
    commands.push("br 1");                      // 1
    commands.push("ldc.i4 4");                  // 2 -- should have skipped this one
    commands.push("ldc.i4 2");                  // 3
    commands.push("add");                       // 4

    vm.ExecuteProgram(commands);

    var result = vm.State.Stack.Top();
    this.Assert(7, result.Value);
};

BranchingTests.prototype.TestBranchEqualTrue = function () {
    var vm = new VM();
    var commands = [];

    commands.push("ldc.i4 5");                  // 0
    commands.push("ldc.i4 5");                  // 1
    commands.push("beq 4");                     // 2 - should skip to offset 7

    commands.push("ldc.i4 1");                  // 3
    commands.push("ldc.i4 2");                  // 4
    commands.push("add");                       // 5
    commands.push("ret");                       // 6

    commands.push("ldc.i4 3");                  // 7
    commands.push("ldc.i4 4");                  // 8
    commands.push("add");                       // 9

    vm.ExecuteProgram(commands);

    var result = vm.State.Stack.Top();
    this.Assert(7, result.Value);
};

BranchingTests.prototype.TestBranchEqualFalse = function () {
    var vm = new VM();
    var commands = [];

    commands.push("ldc.i4 5");                  // 0
    commands.push("ldc.i4 6");                  // 1
    commands.push("beq 1");                     // 2 - should fall to next line and return after 6

    commands.push("ldc.i4 1");                  // 3
    commands.push("ldc.i4 2");                  // 4
    commands.push("add");                       // 5
    commands.push("ret");                       // 6

    commands.push("ldc.i4 1");                  // 7
    commands.push("ldc.i4 2");                  // 8
    commands.push("add");                       // 9

    vm.ExecuteProgram(commands);

    var result = vm.State.Stack.Top();
    this.Assert(3, result.Value);
};

BranchingTests.prototype.TestBranchFalse = function () {
    var vm = new VM();
    var commands = [];

    commands.push("ldc.i4 10");                 // 0
    commands.push("ldc.i4 0");                  // 1
    commands.push("brfalse 1");                 // 2
    commands.push("ldc.i4 4");                  // 3 -- should have skipped this one
    commands.push("ldc.i4 12");                 // 4
    commands.push("add");                       // 5

    vm.ExecuteProgram(commands);

    var result = vm.State.Stack.Top();
    this.Assert(22, result.Value);
};

BranchingTests.prototype.TestBranchFalseButTrue = function () {
    var vm = new VM();
    var commands = [];

    commands.push("ldc.i4 10");                 // 0
    commands.push("ldc.i4 1");                  // 1
    commands.push("brfalse 1");                 // 2
    commands.push("ldc.i4 4");                  // 3
    commands.push("ldc.i4 12");                 // 4
    commands.push("add");                       // 5

    vm.ExecuteProgram(commands);

    var result = vm.State.Stack.Top();
    this.Assert(16, result.Value);
};
